package org.game.msgBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bean.StoreInfo;
import org.bean.StoreInfo.ServerLimit;
import org.cfg.CommodityCfg;
import org.cfg.StoreCfg;
import org.constant.StoreConstant;
import org.game.cache.ServerLimitCommodityBuyRecordCache;
import org.game.db.bean.CommodityBuyRecord;
import org.game.db.bean.Player;
import org.game.db.bean.PlayerStore;
import org.game.db.cache.CommodityBuyRecordCache;
import org.game.db.cache.PlayerStoreCache;
import org.game.manager.StoreManager;
import org.game.protobuf.s2c.S2CStoreMsg;
import org.game.protobuf.s2c.S2CStoreMsg.CommodityBuyLogs;


/**
 * @author : DengYing
 * @CreateDate : 2017年9月13日 上午11:48:06
 * @Description ：Please describe this document
 */
public class StoreMsgBuilder {
    /**
     * 创建单个商城信息
     * @param player
     * @param storeCid
     * @return
     */
    public static S2CStoreMsg.StoreInfo createStoreInfoMsg(Player player, StoreCfg storeCfg){
    	 S2CStoreMsg.StoreInfo.Builder shopInfo = S2CStoreMsg.StoreInfo.newBuilder();

		 PlayerStore store = PlayerStoreCache.me().getPlayerStoreCacheProxy().getStoreByCid(player.getId(),storeCfg.getId());

    	 // 取得具体某个商店
         shopInfo.setCid(storeCfg.getId());
         shopInfo.setTodayRefreshCount(store == null ? 0 : store.getTodayRefreshCount());
         shopInfo.setTotalRefreshCount(store == null ? 0 :store.getTotalRefreshCount());
         shopInfo.setNextRefreshTime(storeCfg.getNextRefreshDate() == null ? 0 : (int) (storeCfg.getNextRefreshDate().getTime() / 1000));
         
         // 取得玩家商城商品
         if (storeCfg.getCommoditySupplyType() == StoreConstant.COMMODITY_SUPPLY_RANDOM_TYPE) {
             List<CommodityCfg> commodityCfgs = StoreManager.me().getPlayerShopCommodity(storeCfg.getId(), player);
             for (CommodityCfg commodityCfg : commodityCfgs) {
                 shopInfo.addCommoditys(commodityCfg.getId());
             }
         }
         return shopInfo.build();
    }
    
    
    /**
     * 创建商店更新信息
     * @param player
     * @param storeCid
     * @return
     */
    public static S2CStoreMsg.RefreshStore createRefreshStoreMsg(Player player, List<StoreCfg> stores) {
        S2CStoreMsg.RefreshStore.Builder builder = S2CStoreMsg.RefreshStore.newBuilder();
        // 商城信息
        for (StoreCfg cfg : stores) {
        	builder.addStores(createStoreInfoMsg(player, cfg));
		}
        return builder.build();
    }

    /**
     * 创建商品购买信息列表(全部)
     *
     * @param player
     * @return
     */
    public static CommodityBuyLogs createBuyRecordInfo(Player player,Set<CommodityCfg> set) {
    	
    	CommodityBuyLogs.Builder builder = CommodityBuyLogs.newBuilder();
    	
    	S2CStoreMsg.StoreCommodityBuyInfo.Builder buyInfo = null;
    	for (CommodityCfg commodityCfg : set) {
    		
			if (commodityCfg.getLimitType() == StoreConstant.LIMIT_SERVER_TYPE) {
				buyInfo = S2CStoreMsg.StoreCommodityBuyInfo.newBuilder();
				buyInfo.setCid(commodityCfg.getId());
				buyInfo.setType(2);
				ServerLimit sr = ServerLimitCommodityBuyRecordCache.me().get(commodityCfg.getId());
				buyInfo.setNowBuyCount(sr == null ? 0 :sr.getNowBuyCount());
				buyInfo.setTotalBuyCount(sr == null ? 0 :sr.getTotalBuyCount());
				builder.addBuyLogs(buyInfo);
			}
			
			if (player!=null) {
				buyInfo = S2CStoreMsg.StoreCommodityBuyInfo.newBuilder();
				buyInfo.setCid(commodityCfg.getId());
				buyInfo.setType(1);
				CommodityBuyRecord commodityBuyRecord = CommodityBuyRecordCache.me().getCommodityBuyRecordCacheProxy().getRecordByPlayerIdCommodityId(player.getId(), commodityCfg.getId());
				buyInfo.setNowBuyCount(commodityBuyRecord == null ? 0 :commodityBuyRecord.getNowBuyCount());
				buyInfo.setTotalBuyCount(commodityBuyRecord == null ? 0 :commodityBuyRecord.getTotalBuyCount());
				builder.addBuyLogs(buyInfo);
			}
		}
        return builder.build();
    }
    
//    
//    /**
//     * 创建商城购买信息(指定商品)
//     * @param player
//     * @param commodityCfgList
//     * @return
//     */
//    public static List<StoreCommodityBuyInfo> createStoreCommodityBuyInfos(Player player,List<CommodityCfg> commodityCfgList){
//    	List<StoreCommodityBuyInfo> StoreCommodityBuyInfos = Lists.newArrayList();
//        commodityCfgList.forEach(cfg -> {
//            CommodityBuyRecord commodityBuyRecord = CommodityBuyRecordCache.me().getByPlayerIdCommodityId(player.getId(), cfg.getId());
//            if (commodityBuyRecord==null) {
//				return;
//			}
//            StoreCommodityBuyInfos.add(createCommodityBuyRecord(commodityBuyRecord));
//        });
//        return StoreCommodityBuyInfos;
//    }
//    
//    /**
//     * 创建商城购买信息(单个)
//     *
//     * @param commodityBuyRecord
//     * @return
//     */
//    private static S2CStoreMsg.StoreCommodityBuyInfo createCommodityBuyRecord(CommodityBuyRecord commodityBuyRecord) {
//        S2CStoreMsg.StoreCommodityBuyInfo.Builder buyInfo = S2CStoreMsg.StoreCommodityBuyInfo.newBuilder();
//        buyInfo.setCid(commodityBuyRecord.getCommodityId());
//        buyInfo.setNowBuyCount(commodityBuyRecord.getNowBuyCount());
//        buyInfo.setTotalBuyCount(commodityBuyRecord.getTotalBuyCount());
//        return buyInfo.build();
//    }
//    
//    /**
//     * 创建商城购买信息(单个)
//     *
//     * @param commodityBuyRecord
//     * @return
//     */
//    public static S2CStoreMsg.StoreCommodityBuyInfo createServerLimitCommodityBuyRecord(ServerLimitCommodityBuyRecord commodityBuyRecord) {
//        S2CStoreMsg.StoreCommodityBuyInfo.Builder buyInfo = S2CStoreMsg.StoreCommodityBuyInfo.newBuilder();
//        buyInfo.setCid(commodityBuyRecord.getCommodityId());
//        buyInfo.setNowBuyCount(commodityBuyRecord.getNowBuyCount());
//        buyInfo.setTotalBuyCount(commodityBuyRecord.getTotalBuyCount());
//        return buyInfo.build();
//    }
//

}
